<?php

use think\Db;

/**
 * 生成订单号
 */
function createOrderNumber()
{
    $number = "HHR" . date('YmdHis') . str_pad(mt_rand(1, 999), 3, '0', STR_PAD_LEFT);
    return $number;
}

/**
 *    作用：产生随机字符串，不长于32位
 */
function createNoncestr($length = 32)
{
    $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
    $str = "";
    for ($i = 0; $i < $length; $i++) {
        $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
    }
    return $str;
}

/**
 * @Author: BarryDragon
 * @Date: 2020-07-31 16:23:29
 * @LastEditors:
 * @name: 获取Accesstoken
 * @msg:
 * @param {type}
 * @return:
 */
function get_accesstoken($app_id, $app_secret)
{
    $acc_token = ''; //初始化
    try {
        $token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $app_id . "&secret=" . $app_secret;
        $token_data = file_get_contents($token_url);
        $token_datas = json_decode($token_data, true);
        // $endtime = time() + 7200;
        // $json_data = json_encode(array('tok' => $token_datas['access_token'], 'time' => $endtime));

        $acc_token = $token_datas['access_token'];
    } finally {
        //返回结果
        return $acc_token;
    }
}

/**
 * @Author: BarryDragon
 * @Date: 2020-07-31 16:26:36
 * @LastEditors:
 * @name: 获取Jsapi_ticket
 * @msg:
 * @param {type}
 * @return:
 */
function get_jsapi_ticket($token)
{
    //初始化
    $ticket = '';
    try {
        $ticket_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" . $token . "&type=jsapi";
        $ticket_data = file_get_contents($ticket_url);
        $ticket_datas = json_decode($ticket_data, true);

        if (empty($ticket_datas['errcode'])) {
            // $endtime = time() + 7200;
            // $json_data = json_encode(array('tok' => $ticket_datas['ticket'], 'time' => $endtime));

            $ticket = $ticket_datas['ticket'];
        }
    } finally {
        //返回结果
        return $ticket;
    }
}

/**
 * @Author: BarryDragon
 * @Date: 2020-07-31 16:23:12
 * @LastEditors:
 * @name: 生成签名
 * @msg:
 * @param {type}
 * @return:
 */
function getSign($Obj)
{
    foreach ($Obj as $k => $v) {
        $Parameters[$k] = $v;
    }
    //签名步骤一：按字典序排序参数
    ksort($Parameters);
    //签名步骤二：URL键值对的格式
    $String = formatBizQueryParaMap($Parameters, false);
    //签名步骤三：sha1加密
    $result_ = sha1($String);

    return $result_;
}

/**
 * @Author: BarryDragon
 * @Date: 2020-07-31 16:22:54
 * @LastEditors:
 * @name: 格式化参数，签名过程需要使用
 * @msg:
 * @param {type}
 * @return:
 */
function formatBizQueryParaMap($paraMap, $urlencode)
{
    $buff = "";
    ksort($paraMap);
    foreach ($paraMap as $k => $v) {
        if ($urlencode) {
            $v = urlencode($v);
        }
        $buff .= strtolower($k) . "=" . $v . "&";
    }
    $reqPar = "";
    if (strlen($buff) > 0) {
        $reqPar = substr($buff, 0, strlen($buff) - 1);
    }
    return $reqPar;
}

/**
 * 获取真实IP
 */
function get_real_ip()
{
    $ip = false;
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ips = explode(', ', $_SERVER['HTTP_X_FORWARDED_FOR']);
        if ($ip) {
            array_unshift($ips, $ip);
            $ip = FALSE;
        }
        for ($i = 0; $i < count($ips); $i++) {
            if (!eregi('^(10│172.16│192.168).', $ips[$i])) {
                $ip = $ips[$i];
                break;
            }
        }
    }
    return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}

// 登录日志
function app_login_logs($user_id)
{
    $data = [
        "user_id" => $user_id,
        "ip" => get_real_ip(),
        "create_time" => time()
    ];
    Db::name("app_login_logs")->insert($data);
}